// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import App from './App';
import router from './router';
import Config from './config/Config';
import User from './service/User';
import vueTap from 'v-tap';

Vue.config.productionTip = false;

Vue.use(vueTap);

// 判断是否登录过
if (!Config.isWeixin()) {
  // alert('请在微信浏览器打开');
  new Vue({
    el: '#app',
    router,
    // template: '<App/>',
    // components: {App}
    render: h => h(App)
  });
} else {
  // 有登录缓存不做登录
  if (localStorage.getItem('FINISH_LOGIN') === 'true') {
    new Vue({
      el: '#app',
      router,
      // template: '<App/>',
      // components: {App}
      render: h => h(App)
    });
  } else {
    let code = Config.getUrlPara('code');
    // let APIUrl = Config.getAPIUrl('base_login');
    if (!code) {
      // 地址栏里没有code 信息则重定向去微信静默授权
      let redirectUri = Config.getRedirectUri();
      let scope = 'snsapi_base'; // snsapi_userinfo;
      let url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + Config.getAppId() + '&redirect_uri=' + redirectUri + '&response_type=code' + '&scope=' + scope + '&state=minic&connect_redirect=1#wechat_redirect';
      window.location.href = url;
    } else {
      User.getWxUserInfoFromServer(code).then(
        function (user) {
          User.onGetWxInfoSuccess(user)
          /* eslint-disable no-new */
          new Vue({
            el: '#app',
            router,
            // template: '<App/>',
            // components: {App}
            render: h => h(App)
          });
        }
      ).catch(
        function () {
          alert('sorry, 没有获得您的信息,请刷新页面重试')
        }
      )
    }
  }
}
